using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using TimeSheet.Protected.View.Includes;

namespace TimeSheet.Protected
{
    /// <summary>
    /// A nice description of BaseAdminForm goes here.
    /// 
    /// Version: $Id: BaseAdminForm.cs,v 1.2 2004/05/18 15:32:24 jperrin Exp $
    /// 
    /// Date: 
    /// Copyright: 2004, Kanga Communications, Calgary, Alberta, Canada. 
    /// </summary>
    public class BaseAdminForm : Page
    {
        private string _pageTitle;
        private string _cssFile;
        private HtmlGenericControl _headerDiv;
        private HtmlGenericControl _sidebarDiv;

        public BaseAdminForm()
        {
            _pageTitle = "Timesheet";
            _cssFile = "admin.css";

            _headerDiv = new HtmlGenericControl("div");
            _headerDiv.Attributes.Add("id", "Header");

            _sidebarDiv = new HtmlGenericControl("div");
            _sidebarDiv.Attributes.Add("id", "SideBar");
        }

        protected virtual void LoadDefaultLookAndFeel()
        {
            Header header = (Header) LoadControl("View/Includes/Header.ascx");
            header.LoadMe();
            _headerDiv.Controls.Add(header);
            _sidebarDiv.Controls.Add(LoadControl("View/Includes/SideBar.ascx"));
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (Session[AppSettings.AdministratorSessionName] == null)
            {
                Response.Redirect("Login.aspx");
            }
            RenderDocType(writer); //write the doctype declaration
            writer.Write("<html>\n");
            RenderMetaInfo(writer);
                //holds the <head></head> tags, and everything in between.
            BodyStart(writer); //define the <body> tag
            RenderHeader(writer);
            RenderSideBar(writer);
            RenderContent(writer);
            RenderFooter(writer);
            BodyEnd(writer);
            writer.Write("</html>");
        }

        #region Content Renderers

        protected virtual void RenderDocType(HtmlTextWriter writer)
        {
            writer.Write(
                "<!doctype html public \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n");
        }

        protected virtual void RenderMetaInfo(HtmlTextWriter writer)
        {
            writer.Write(
                "\t<head>\n" +
                "\t\t<title>" + _pageTitle + "</title>\n" + //set the title of the page
                "\t\t<link href=\"" + _cssFile +
                "\" type=\"text/css\" rel=\"STYLESHEET\">\n" +
                "\t\t<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">\n" +
                //set the css file
                "\t</head>\n");
        }

        protected virtual void BodyStart(HtmlTextWriter writer)
        {
            writer.Write("\t<body>\n");
        }

        protected virtual void RenderHeader(HtmlTextWriter writer)
        {
            _headerDiv.RenderControl(writer);
        }

        protected virtual void RenderSideBar(HtmlTextWriter writer)
        {
            _sidebarDiv.RenderControl(writer);
        }

        protected virtual void RenderContent(HtmlTextWriter writer)
        {
            writer.Write("\n<div id=\"Content\">\n");
            base.Render(writer);
            writer.Write("\n</div>\n");
        }

        protected virtual void RenderFooter(HtmlTextWriter writer)
        {
            //We don't have a footer for this project.
        }

        protected virtual void BodyEnd(HtmlTextWriter writer)
        {
            writer.Write("\t</body>\n");
        }

        #endregion

        #region Content Modifiers

        protected virtual void AddToHeader(Control control)
        {
            _headerDiv.Controls.Add(control);
        }

        protected virtual void AddToSideBar(Control control)
        {
            _sidebarDiv.Controls.Add(control);
        }

        #endregion

        #region Page Properties

        protected string PageTitle
        {
            get { return _pageTitle; }
            set { _pageTitle = value; }
        }

        protected string CssFile
        {
            get { return _cssFile; }
            set { _cssFile = value; }
        }

        #endregion

        #region Helper Functions

        protected virtual long RequestedEntityID
        {
            get
            {
                if (Request["id"] == null)
                {
                    return -1;
                }
                return Convert.ToInt64(Request["id"]);
            }
        }

        protected virtual TimeSheetBLL.Administrator CurrentAdministrator
        {
            get { return Session["Administrator"] as TimeSheetBLL.Administrator; }
        }

        #endregion
    }
}